﻿using System;
using System.Windows.Forms;
using System.Security.Principal;
using System.Threading;

namespace WinFormAuthentication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void loginButton_Click(object sender, EventArgs e)
        {
            if (tbUserName.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名!");
                tbUserName.Focus();
                return;
            }

            IIdentity _identity = new GenericIdentity(tbUserName.Text.Trim());
            IPrincipal _principal = new GenericPrincipal(_identity, new string[] { "管理员" });

            // 将其附加到当前线程的 CurrentPrincipal
            Thread.CurrentPrincipal = _principal;

            MessageBox.Show("登录成功!");
        }

        private void logoutButton_Click(object sender, EventArgs e)
        {
            Thread.CurrentPrincipal = null;
            MessageBox.Show("已经退出!");
        }

        private void showButton_Click(object sender, EventArgs e)
        {
            new Form2().ShowDialog();
        }
    }
}
